//
//  SearchTravelTableViewCell.m
//  BTRIP
//
//  Created by Rain on 15/11/19.
//  Copyright © 2015年 johnbk. All rights reserved.
//

#import "SearchTravelTableViewCell.h"
#import "SearchModel.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>

@implementation SearchTravelTableViewCell

- (void)dealloc{
    [_frontImage release];
    [_nameLabel release];
    [_allLabel release];
    [_userImage release];
    [_smallLabel release];
    [_userLabel release];
    [super dealloc];
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.frontImage = [[UIImageView alloc]init];
        //        self.frontImage.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:self.frontImage];
        [_frontImage release];
        
        self.smallLabel = [[UILabel alloc]init];
        self.smallLabel.backgroundColor = [UIColor blackColor];
        self.smallLabel.alpha = 0.01;
        [self.contentView addSubview:self.smallLabel];
        [_smallLabel release];
        
        self.nameLabel = [[UILabel alloc]init];
        //        self.nameLabel.backgroundColor = [UIColor brownColor];
        [self.contentView addSubview:self.nameLabel];
        [_nameLabel release];
        
        self.allLabel = [[UILabel alloc]init];
        //        self.allLabel.backgroundColor = [UIColor orangeColor];
        [self.contentView addSubview:self.allLabel];
        [_allLabel release];
        
        self.userLabel = [[UILabel alloc]init];
        self.userLabel.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.userLabel];
        [_userLabel release];
        
        self.userImage = [[UIImageView alloc]init];
        //        self.userImage.backgroundColor = [UIColor blueColor];
        [self.contentView addSubview:self.userImage];
        [_userImage release];
        
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.frontImage.frame = CGRectMake(10 * SWIDTH, 5 * SWIDTH, KWIDTH - 20 * SWIDTH, self.frame.size.height - 10 * SWIDTH);
    self.frontImage.layer.cornerRadius = 5;
    self.frontImage.layer.masksToBounds = YES;
    self.smallLabel.frame = CGRectMake(self.frontImage.frame.origin.x, self.frontImage.frame.origin.y, self.frontImage.frame.size.width, self.frontImage.frame.size.height);
    self.nameLabel.frame = CGRectMake(20 * SWIDTH, 15 * SWIDTH, KWIDTH - 40 * SWIDTH, 60 * SWIDTH);
    self.allLabel.frame = CGRectMake(self.nameLabel.frame.origin.x, self.nameLabel.frame.origin.y + self.nameLabel.frame.size.height - 20 * SWIDTH, self.nameLabel.frame.size.width, 30 * SWIDTH);
    self.userLabel.frame = CGRectMake(self.nameLabel.frame.origin.x + 2 * SWIDTH, self.allLabel.frame.size.height + self.allLabel.frame.origin.y + 80 * SWIDTH, 56 * SWIDTH, 56 * SWIDTH);
    self.userLabel.layer.cornerRadius = self.userLabel.frame.size.width / 2;
    self.userLabel.layer.masksToBounds = YES;
    self.userImage.frame = CGRectMake(0,0, 52 * SWIDTH, 52 * SWIDTH);
    self.userImage.center = self.userLabel.center;
    self.userImage.layer.cornerRadius = self.userImage.frame.size.width / 2;
    self.userImage.layer.masksToBounds = YES;
    
}

- (void)setTravel:(SearchModel *)travel{
    if (_travel != travel) {
        [_travel release];
        _travel = [travel retain];
    }
    
    NSURL *url1 = [NSURL URLWithString:[NSString stringWithFormat:@"%@", travel.front_cover_photo_url]];
    [self.frontImage sd_setImageWithURL:url1 placeholderImage:[UIImage imageNamed:@"background"]];
    self.nameLabel.text = travel.name;
    self.nameLabel.numberOfLines = 0;
    [self.nameLabel setFont:[UIFont systemFontOfSize:20]];
    self.nameLabel.textColor = [UIColor whiteColor];
    self.allLabel.textColor = [UIColor whiteColor];
    [self.allLabel setFont:[UIFont systemFontOfSize:14]];
    self.allLabel.text = [NSString stringWithFormat:@"%@/ %@天/ %@图",travel.start_date,travel.days,travel.photos_count];
    
    NSURL *url2 = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[travel.user objectForKey:@"image"]]];
    [self.userImage sd_setImageWithURL:url2 placeholderImage:[UIImage imageNamed:@"background"]];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
